package com.snts.synchronization.protocol.adapter;

import static com.snts.synchronization.core.Column.b;
import static com.snts.synchronization.core.XMLLoader.us;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import nu.xom.Attribute;
import nu.xom.Element;

import com.snts.synchronization.core.Change;
import com.snts.synchronization.core.ChangeColumn;
import com.snts.synchronization.core.ExternalTable;
import com.snts.synchronization.core.SyncRequest;
import com.snts.synchronization.core.SyncResponse;
import com.snts.synchronization.core.SyncTable;
import com.snts.synchronization.core.Synchronization;
import com.snts.synchronization.core.TimeStampWithTimeZone;
import com.snts.synchronization.core.UUID;
import com.snts.synchronization.protocol.ProtocolException;
import com.snts.synchronization.protocol.XMLConstants;

public class SyncRequestPA extends SyncPackagePA<SyncRequest> implements XMLConstants {
	
	private static final Log log = LogFactory.getLog(SyncRequestPA.class);
	
	private SyncRequest syncRequest;
	private DefaultHandler syncPackageParser;
	private Synchronization synchronization;

	public void setSynchronization(Synchronization synchronization) {
		this.synchronization = synchronization;
	}

	@Override
	protected SyncRequest popMessage() {
		return syncRequest;
	}

	@Override
	protected Element getMessageXOM(SyncRequest message) {
		Element root = new Element("sync-request");
		root.addAttribute(new Attribute("session-id", message.getSessionId()));
		addItems(message, root);
		return root;
	}

	@Override
	public void characters(char[] ch, int start, int length) throws SAXException {
		if(log.isDebugEnabled()){
			log.debug("characters");
			log.debug("ch=[" + new String(ch, start, length) + "]");
		}
		super.characters(ch, start, length);
		if(syncPackageParser != null){
			syncPackageParser.characters(ch, start, length);
		}
	}

	@Override
	protected void doEndElement(String uri, String localName, String qName, String textValue) throws SAXException {
		if(SYNC_REQUEST.equals(qName)){
			syncPackageParser = null;
		} else {
			syncPackageParser.endElement(uri, localName, qName);
		}
	}

	@Override
	public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
		if(log.isDebugEnabled()){
			
		}
		super.startElement(uri, localName, qName, attributes);
		if(syncPackageParser == null && !SYNC_REQUEST.equals(qName)){
			String errorMessage = "invalid start element for Synchronization Response \"" + qName + "\"";
			log.fatal(errorMessage);
			throw new SyncParseException(errorMessage);
		} if(SYNC_REQUEST.equals(qName)){
			syncRequest = new SyncRequest();
			syncRequest.setSessionId(attributes.getValue("session-id"));
			syncPackageParser = getSyncPackageParser(syncRequest, synchronization);
		} else {
			syncPackageParser.startElement(uri, localName, qName, attributes);
		}
	}

	public static void main(String[] args) throws SQLException, UnsupportedEncodingException, IOException {

/*		Connection conn = DriverManager.getConnection("jdbc:postgresql://127.0.0.1:5432/Restaurant", "postgres", "123");
		Synchronization synchronization = new Synchronization();
		synchronization.load(conn.getMetaData());

		SyncRequestPA syncRequestPA = new SyncRequestPA();
		syncRequestPA.setSynchronization(synchronization);
		SyncRequest syncRequest = syncRequestPA.parseMessage(new ByteArrayInputStream());

		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		syncRequestPA.serializeMessage(syncRequest, baos);

		System.out.println(new String(baos.toByteArray()));*/

	}


}
